/**
 * @fileOverview
 * @exports MacroCommand as org.puremvc.js.patterns.observer.MacroCommand
 * @imports org.puremvc.js.adapters.NotifierAdapter
 */

/**
 * @constructor
 * @extends {Notifier}
 */
function MacroCommand ()
{
	this.subCommands= [];
	this.initializeMacroCommand();
};

// perform subclassing
MacroCommand.prototype= new NotifierAdapter;
MacroCommand.prototype.constructor= MacroCommand;

/**
 * @protected
 * @type {Array.<SimpleCommand|MacroCommand>}
 */
MacroCommand.prototype.subCommands= null;

/**
 * @protected
 * @return {void}
 */
MacroCommand.prototype.initializeMacroCommand= function ()
{
};

/**
 * @protected
 * @param {Function} commandCtorRef
 * @return {void}
 */
MacroCommand.prototype.addSubCommand= function (commandCtorRef)
{
	this.subCommands.push(commandCtorRef);
};

/**
 * @todo OPTIMIZE SUBCOMMAND ITERATION!!!!
 * @final
 * @param {Notification} notification
 * @return {void}
 */
MacroCommand.prototype.execute= function (notification)
{
	while (this.subCommands.length > 0)
	{
		var commandClassCtor= this.subCommands.shift();
		var commandInstance= new commandClassCtor();
		commandInstance.execute(notification);
	}

	return;

	// // this is an optimal take on the above implementation
	// // even though more verbose, it will perform faster and
	// // is safe, taking into account any errors that may be
	// // thrown during the course of dispatching command
	//
	// /** @type {Array.<Function>} */
	// var subCommands= this.subCommands
	//
	// /** @type {Function} */
	// ,	commandCtorRef
	//
	// /** @type {ICommand} */
	// ,	command
	//
	// /** @type {Error|null} */
	// ,	executionError;
	//
	// try
	// {
	// // wrap iteration in a try catch block. This ensures that
	// // if an error is encountered while executing a command
	// // that we still have the opportunity to release memory
	// // by destroying references to ICommand constructors
	// for (var i= 0, n= subCommands.length; i < n; i++)
	// {
	// commandCtorRef= subCommands[i];
	// command= new commandCtorRef;
	// command.execute(notification);
	// };
	// }
	// catch (thrown)
	// {
	// // record the error
	// executionError= thrown;
	// }
	// finally
	// {
	// // release the commands in all cases
	// delete this.subCommands;
	// }
	//
	// if (executionError)
	// {
	// // if an execution error was thrown, rethrow it
	// throw executionError;
	// }
};